﻿using System;
using System.Windows.Forms;
using System.Security.Permissions;
using System.IO;
using System.Drawing;

namespace nathan之文件系统监视
{
    public partial class Form1 : Form
    {
        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]     
        public Form1()
        {
            InitializeComponent(); 
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)  //开始监控
        {
            watcher.Path = tb_watch_path .Text ;     //设置监控的文件夹
            watcher.EnableRaisingEvents = true;
            watcher.IncludeSubdirectories = true;   //是否包含子目录
            btn_watch_start.Enabled = false;
            btn_watch_stop.Enabled = true;
        }

        private void btnbrowse_Click(object sender, EventArgs e)
        {
          if (DialogResult .OK ==  folderBrowserDialog1.ShowDialog())
            tb_watch_path.Text = folderBrowserDialog1.SelectedPath.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            watcher.EnableRaisingEvents = false;
            btn_watch_start.Enabled = true;
            btn_watch_stop.Enabled = false;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://zybsoft.com");
        }

        private void btnclear_Click(object sender, EventArgs e)
        {
            lb_created.Items.Clear();
            lb_deleted.Items.Clear();
            lb_modified.Items.Clear();
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(lb_created.Text);
            }
            catch { }
        }

        private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
        {
            lb_modified.Items.Add(e.FullPath);
            lb_modified.SelectedIndex = lb_modified.Items.Count - 1;
        }

        private void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
        {
            lb_created.Items.Add(e.FullPath );
            lb_created.SelectedIndex = lb_created.Items.Count - 1;
        }

        private void fileSystemWatcher1_Deleted(object sender, FileSystemEventArgs e)
        {
            lb_deleted.Items.Add(e.FullPath);
            lb_deleted.SelectedIndex = lb_deleted.Items.Count - 1;
        }

        private void fileSystemWatcher1_Renamed(object sender, RenamedEventArgs e)
        {
            lb_renamed.Items.Add(e.FullPath);
            lb_renamed.SelectedIndex = lb_renamed.Items.Count - 1;
        }

        private void lb_modified_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(lb_modified.Text);
            }
            catch { }
        }

        private void lb_deleted_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(lb_deleted.Text);
            }
            catch { }
        }

        private void lb_renamed_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(lb_renamed.Text);
            }
            catch { }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            watcher.EnableRaisingEvents = false;
        }

        private void tb_watch_path_TextChanged(object sender, EventArgs e)
        {
            watcher.Path = tb_watch_path.Text;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("More visit on http://zybsoft.com");
        }

    }
  
}
